{% extends "stocks/base.html" %}

{% block title %}{{ title }}{% endblock %}

{% block content %}
<div class="container-fluid">
    <h1 class="mb-4">{{ title }}</h1>
    
    <div class="row">
        <!-- 高强度信号卡片 -->
        <div class="col-md-6">
            <div class="card">
                <div class="card-header bg-primary text-white">
                    <h5 class="mb-0">近期高强度买入信号</h5>
                </div>
                <div class="card-body">
                    {% if strong_signals %}
                        <div class="list-group">
                            {% for signal in strong_signals %}
                                <div class="list-group-item">
                                    <div class="d-flex justify-content-between align-items-center">
                                        <div>
                                            <h6 class="mb-1">{{ signal.symbol }} - {{ signal.stock_name }}</h6>
                                            <small class="text-muted">{{ signal.strategy_name }}</small>
                                        </div>
                                        <span class="badge bg-{% if signal.signal_strength >= 80 %}danger{% elif signal.signal_strength >= 60 %}warning{% else %}info{% endif %}">
                                            强度: {{ signal.signal_strength|floatformat:1 }}
                                        </span>
                                    </div>
                                    <p class="mb-1 small">{{ signal.reason }}</p>
                                    <small class="text-muted">日期: {{ signal.signal_date }} | 价格: {{ signal.signal_price|floatformat:2 }}</small>
                                </div>
                            {% endfor %}
                        </div>
                    {% else %}
                        <p class="text-muted">暂无高强度买入信号</p>
                    {% endif %}
                </div>
            </div>
        </div>

        <!-- 策略表现统计 -->
        <div class="col-md-6">
            <div class="card">
                <div class="card-header bg-success text-white">
                    <h5 class="mb-0">策略表现统计 (最近30天)</h5>
                </div>
                <div class="card-body">
                    {% if strategy_performance %}
                        <div class="table-responsive">
                            <table class="table table-sm">
                                <thead>
                                    <tr>
                                        <th>策略名称</th>
                                        <th>信号数量</th>
                                        <th>平均强度</th>
                                        <th>类型</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for item in strategy_performance %}
                                        <tr>
                                            <td>
                                                <a href="{% url 'strategy_detail' item.strategy.id %}">
                                                    {{ item.strategy.name }}
                                                </a>
                                            </td>
                                            <td>
                                                <span class="badge bg-primary">{{ item.stats.total_signals }}</span>
                                            </td>
                                            <td>
                                                <span class="badge bg-{% if item.stats.avg_strength >= 70 %}danger{% elif item.stats.avg_strength >= 50 %}warning{% else %}secondary{% endif %}">
                                                    {{ item.stats.avg_strength|floatformat:1 }}
                                                </span>
                                            </td>
                                            <td>
                                                <span class="badge bg-info">{{ item.strategy.get_strategy_type_display }}</span>
                                            </td>
                                        </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>
                    {% else %}
                        <p class="text-muted">暂无策略统计数据</p>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>

    <div class="row mt-4">
        <div class="col-12">
            <div class="card">
                <div class="card-header bg-info text-white">
                    <h5 class="mb-0">快速操作</h5>
                </div>
                <div class="card-body">
                    <div class="btn-group" role="group">
                        <a href="{% url 'strategy_list' %}" class="btn btn-outline-primary">
                            <i class="bi bi-list-ul"></i> 查看所有策略
                        </a>
                        <a href="{% url 'buy_signals' %}" class="btn btn-outline-success">
                            <i class="bi bi-graph-up"></i> 查看买入信号
                        </a>
                        <a href="{% url 'analyze_all_stocks' %}" class="btn btn-outline-warning" onclick="return confirm('确定要分析所有股票吗？这可能需要一些时间。')">
                            <i class="bi bi-play-circle"></i> 分析所有股票
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="row mt-4">
        <div class="col-md-6">
            <div class="card">
                <div class="card-header bg-warning text-dark">
                    <h5 class="mb-0">系统状态</h5>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-6">
                            <div class="text-center">
                                <h3>{{ strategy_performance|length }}</h3>
                                <small class="text-muted">启用策略数量</small>
                            </div>
                        </div>
                        <div class="col-6">
                            <div class="text-center">
                                <h3>
                                    {% with total=0 %}
                                        {% for item in strategy_performance %}
                                            {% if item.stats.total_signals %}
                                                {% with total=total|add:item.stats.total_signals %}{% endwith %}
                                            {% endif %}
                                        {% endfor %}
                                        {{ total }}
                                    {% endwith %}
                                </h3>
                                <small class="text-muted">总信号数量</small>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-md-6">
            <div class="card">
                <div class="card-header bg-secondary text-white">
                    <h5 class="mb-0">数据分析工具</h5>
                </div>
                <div class="card-body">
                    <form method="get" action="{% url 'buy_signals' %}" class="row g-2">
                        <div class="col-md-6">
                            <input type="text" name="symbol" class="form-control" placeholder="输入股票代码..." value="{{ request.GET.symbol }}">
                        </div>
                        <div class="col-md-4">
                            <select name="min_strength" class="form-select">
                                <option value="50">强度≥50</option>
                                <option value="60">强度≥60</option>
                                <option value="70">强度≥70</option>
                                <option value="80">强度≥80</option>
                            </select>
                        </div>
                        <div class="col-md-2">
                            <button type="submit" class="btn btn-primary w-100">搜索</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
// 自动刷新页面数据（可选）
setTimeout(function() {
    window.location.reload();
}, 300000); // 5分钟刷新一次

// 策略类型颜色映射
const strategyColors = {
    'ma': 'primary',
    'rsi': 'success', 
    'macd': 'warning',
    'boll': 'info',
    'pe': 'secondary',
    'dividend': 'dark',
    'roe': 'danger'
};
</script>
{% endblock %}
